package com.tz.cartelo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tz.cartelo.R;
import com.tz.cartelo.adapter.MyOrderAdapter;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.result.ResultGoodsClassifyDetails;
import com.tz.cartelo.util.AES;
import com.tz.cartelo.util.CommonUtils;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/4/16.
 */

public class MyOrderActivity extends DataLoadActivity implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    private static final String TAG = "MyOrderActivity";
    private BGARefreshLayout mRefreshLayout;
    private Api mApi;
    private int mPageNo;
    private MyOrderAdapter mMyOrderAdapter;
    private List<GoodsList> mGoodsListList;
    private boolean mCanLoadMore = true;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void init() {
        mRefreshLayout = (BGARefreshLayout) findViewById(R.id.fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, this);

        ListView myOrderList = (ListView) findViewById(R.id.my_order_list);
        mGoodsListList = new ArrayList<>();
        mMyOrderAdapter = new MyOrderAdapter(this, mGoodsListList);
        myOrderList.setAdapter(mMyOrderAdapter);
        myOrderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString(Key.ORDER_NO, mGoodsListList.get(i).getOrderNo());
                switchActivity(MyOrderDetailsActivity.class, bundle);
            }
        });

        mPageNo = 1;
        mApi = Api.MY_ORDER;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {
        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.manager_center_system_order);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case MY_ORDER:
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                params.addParam("pageNo", mPageNo + "");
                String time = System.currentTimeMillis() + "";
                params.addParam("time", time);

                List<String> dataList = new ArrayList<>();
                dataList.add(PreferencesUtils.getString(Key.PER_USER_ID, ""));
                dataList.add(time);
                try {
                    params.addParam("sign", AES.createASE(dataList));
                } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case MY_ORDER:
                //结束刷新和加载
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endLoadingMore();

                ResultGoodsClassifyDetails resultGoodsClassifyDetails = fromJson(response, ResultGoodsClassifyDetails.class);
                if (!resultGoodsClassifyDetails.isSuccess()) {
                    showToast(resultGoodsClassifyDetails.getMessage());
                    return;
                }
                mGoodsListList.addAll(resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList());
                mMyOrderAdapter.notifyDataSetChanged();

//                if (resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList().isEmpty() && mGoodsListList.isEmpty())
//                    mGoodsClassifyNoGoodsLayout.setVisibility(View.VISIBLE);
//                else
//                    mGoodsClassifyLayout.setVisibility(View.VISIBLE);

                if (mPageNo > 1) {
                    if (resultGoodsClassifyDetails.getGoodsClassifyDetails().getGoodsListList().isEmpty()) {
                        mCanLoadMore = false;
                        mPageNo--;
                    }
                }

                break;
            default:
                break;
        }
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mPageNo = 1;
        mGoodsListList.clear();
        mCanLoadMore = true;
        loadData(Api.MY_ORDER, true);
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (!mCanLoadMore) {
            mRefreshLayout.endLoadingMore();
            return false;
        } else {
            mPageNo++;
            loadData(Api.MY_ORDER, true);
            return true;
        }
//        mRefreshLayout.endLoadingMore();
//        return false;
    }

}
